package com.uvnos4j.matilda.security.callback;

import com.uvnos4j.matilda.commons.dto.Result;
import com.uvnos4j.matilda.commons.utils.Const;
import com.uvnos4j.matilda.commons.utils.RequestUtil;
import com.uvnos4j.matilda.commons.utils.ResponseUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 无权限回调
 * <p>
 * Description:无权限回调
 * </p>
 *
 * @author Guo.wl
 * @version v1.0.0
 * @since 2020-08-19 23:25:22
 */
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException)
            throws IOException, ServletException {

        if (RequestUtil.isAjax()) {
            ResponseUtil.writeJson(Result.fail("您没有改功能的操作权限"));
        } else {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            response.setCharacterEncoding("utf-8");
            response.sendRedirect(Const.AUTH_ACCESS_DENIED_URL);
        }
    }

}